package com.cskaoyan.controller;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.sysmanage.AddRoleBo;
import com.cskaoyan.bean.sysmanage.InsertPermissionBo;
import com.cskaoyan.bean.sysmanage.SystemPermissionsVO;
import com.cskaoyan.bean.sysmanage.UpdateRoleBo;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.PermissionService;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.service.SystemPermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @Autowired
    AdminService adminService;

    @Autowired
    SystemPermissionService systemPermissionService;

    @Autowired
    PermissionService permissionService;

    //角色管理信息
    @RequiresPermissions(value = "admin:role:list")
    @RequestMapping("list")
    public BaseRespVo getRoleList(String name, BaseParam baseParam) {
        BasePageData pageDate = roleService.queryRoleList(name, baseParam);
        return BaseRespVo.ok(pageDate);
    }

    //添加角色
    @RequiresPermissions(value = "admin:role:create")
    @RequestMapping("create")
    public BaseRespVo createRole(@RequestBody AddRoleBo addRoleBo) {
        Role role = roleService.selectByName(addRoleBo.getName());
        if (role != null) {
            return BaseRespVo.fail("当前角色名称已存在，请更换名称");
        }
        Role addRole = roleService.addRole(addRoleBo.getName(), addRoleBo.getDesc());
        return BaseRespVo.ok(addRole);
    }


    //修改角色
    @RequiresPermissions(value = "admin:role:update")
    @RequestMapping("update")
    public BaseRespVo updateRole(@RequestBody UpdateRoleBo updateRoleBO) {
        Role role = roleService.selectByName(updateRoleBO.getName());
        if (role != null && !role.getId().equals(updateRoleBO.getId())) {
            return BaseRespVo.fail("当前角色名称已存在，请更换名称");
        }
        //用户名不重复或未修改
        boolean result = roleService.updateRole(updateRoleBO);
        if (result) return BaseRespVo.ok();
        return BaseRespVo.fail("修改角色失败");
    }

    //删除角色
    @RequiresPermissions(value = "admin:role:delete")
    @RequestMapping("delete")
    public BaseRespVo deleteRole(@RequestBody UpdateRoleBo updateRoleBo) {
        List<Admin> adminList = adminService.selectAdminByRolesId(updateRoleBo.getId());
        if (adminList.size() != 0) {
            return BaseRespVo.fail("当前角色存在管理员用户，不能删除");
        }
        boolean result = roleService.deleteRoleById(updateRoleBo.getId());
        return result ? BaseRespVo.ok() : BaseRespVo.fail("删除失败");
    }

    //权限编辑 显示所有菜单
    @RequiresPermissions(value = "admin:role:permission:get")
    @GetMapping("permissions")
    public BaseRespVo getPermissionsInfo(Integer roleId) {
        SystemPermissionsVO systemPermissionsVO = systemPermissionService.queryByRoleId(roleId);
        return BaseRespVo.ok(systemPermissionsVO);
    }


    //授权
    @RequiresPermissions(value = "admin:role:permission:update")
    @PostMapping("permissions")
    public BaseRespVo updatePermission(@RequestBody InsertPermissionBo insertPermissionBo) {
        boolean result = permissionService.updatePermission(insertPermissionBo);
        return result ? BaseRespVo.ok() : BaseRespVo.fail("授权失败");
    }

    //为SystemPermission表中插入数据
    @RequestMapping("insert")
    public BaseRespVo insertSystemPermission(@RequestBody SystemPermissionsVO systemPermissionsVO) {
        boolean result = systemPermissionService.insertSystemPermission(systemPermissionsVO);
        return result ? BaseRespVo.ok() : BaseRespVo.fail("插入数据失败");
    }
}
